package com.project.internship.service.impl;

import com.project.internship.entity.BaseEvaluate;
import com.project.internship.mapper.BaseEvaluateMapper;
import com.project.internship.service.BaseEvaluateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.internship.utils.UserUtils;
import com.project.internship.utils.YearUtils;
import com.project.internship.vo.BaseEvaluateVO;
import com.project.internship.vo.BaseTotalEvaluateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class BaseEvaluateServiceImpl extends ServiceImpl<BaseEvaluateMapper, BaseEvaluate> implements BaseEvaluateService {

    @Autowired
    BaseEvaluateMapper beMapper;

    @Override
    public List<BaseEvaluateVO> getAllotBaseEva() {
        return beMapper.getAllotBaseEva(UserUtils.getCurrentUser().getTeacherId(),YearUtils.getCurrentYear());
    }

    @Override
    public Integer addBaseEva(BaseEvaluate baseEvaluate) {
        return beMapper.insert(baseEvaluate);
    }

    @Override
    public List<BaseTotalEvaluateVO> getThisYearBaseEva(String keyword) {
        return beMapper.getThisYearBaseEva(UserUtils.getCurrentUser().getDepartmentId(), YearUtils.getCurrentYear(), keyword);
    }

    @Override
    public List<BaseTotalEvaluateVO> getBaseAvgEva(String keyword) {

        List<BaseTotalEvaluateVO> records = beMapper.getBaseAvgEva(UserUtils.getCurrentUser().getDepartmentId(), keyword);

        List<BaseTotalEvaluateVO> list = new ArrayList<>();
        List<BaseTotalEvaluateVO> need = new ArrayList<>();
        List<Integer> ids = new ArrayList<>();

        for (BaseTotalEvaluateVO record : records) {
            if(ids.contains(record.getId())) {
                continue;
            }else {
                ids.add(record.getId());

                for (BaseTotalEvaluateVO avg : records) {
                    int id = avg.getId();
                    if(record.getId().equals(id)) {
                        need.add(avg);
                    }
                }

                double total = 0;
                double num = 0;
                double avgScore = 0;

                for (BaseTotalEvaluateVO score : need) {
                    total += score.getThisYearScore();
                    num ++;
                }

                avgScore = total / num;
                BigDecimal bd = new BigDecimal(avgScore);
                double value = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                record.setAvgScore(value);
                record.setNumber((int) num);

                list.add(record);
                need.clear();
            }

        }

        list.sort(new Comparator<BaseTotalEvaluateVO>() {
            @Override
            public int compare(BaseTotalEvaluateVO o1, BaseTotalEvaluateVO o2) {
                if(o1.getAvgScore() == null || o2.getAvgScore() == null){
                    return 0;
                }
                return o2.getAvgScore().compareTo(o1.getAvgScore());
            }
        });

        return list;
    }

}
